package com.hz.eduService.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.apiService.client.TeacherApi;
import com.hz.apiService.model.domain.StudentDTO;
import com.hz.apiService.model.domain.TeacherDTO;
import com.hz.apiService.model.enums.ApiResponseCodeEnum;
import com.hz.apiService.model.request.ApiResponse;
import com.hz.eduService.domain.Course;
import com.hz.eduService.service.CourseService;
import com.hz.eduService.mapper.CourseMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author li
 * @description 针对表【courses(课程表)】的数据库操作Service实现
 * @createDate 2023-09-23 17:13:58
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Resource
    CourseMapper courseMapper;
    @Resource
    TeacherApi teacherApi;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public int addCourse(String courseName, String courseCode, Integer teacherId, Integer sectionCount, String courseTime, Integer maxStudentCount, String courseProfile, String textbook, Integer creditsCount, Integer allScore, Integer passScore, Integer isDeleted) {
        Course course = new Course();
        course.setCourseName(courseName);
        course.setCourseCode(courseCode);
        course.setTeacherUserId(teacherId);
        // 获取教师信息
        ApiResponse res = JSON.parseObject(teacherApi.getTeacherInfo(teacherId), ApiResponse.class);
        if (!res.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())) {
            return 0;
        }
        TeacherDTO teacherDTO = JSON.parseObject(JSON.toJSONString(res.getData()),TeacherDTO.class);
        course.setTeacherRealName(teacherDTO.getRealname());
        course.setSectionCount(sectionCount);
        course.setCourseTime(courseTime);
        course.setMaxStudentCount(maxStudentCount);
        course.setCourseProfile(courseProfile);
        course.setTextbook(textbook);
        course.setCreditsCount(creditsCount);
        course.setAllScore(allScore);
        course.setPassScore(passScore);
        course.setIsDeleted(isDeleted);

        return courseMapper.insert(course);
    }

    @Override
    @CacheEvict(value = "teacherCourses", key = "#course.teacherUserId")
    public boolean editCourse(Course course) {
        if (courseMapper.selectById(course.getId()) == null) return false;
        return courseMapper.updateById(course) != 0;
    }

    @Override
    public boolean deleteCourse(Integer id) {
        if (courseMapper.selectById(id) == null) {
            return false;
        }
        return courseMapper.deleteById(id) != 0;
    }

    @Override
    public Course viewCourse(Integer id) {

        return courseMapper.selectById(id);
    }


    @Override
    @Cacheable(value = "allCourses")
    public List<Course> searchAllCourses() {
        List<Course> result = courseMapper.selectList(null);
        return result;
    }

    @Override
    @Cacheable(value = "course_", key = "#courseCode")
    public Course searchCourseByCode(String courseCode) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_code", courseCode);
        return courseMapper.selectOne(queryWrapper);
    }

    @Override
    public Course simpleInfoCourse(Course course) {
        Course simpleCourse = new Course();
        simpleCourse.setCourseCode(course.getCourseCode());
        simpleCourse.setCourseName(course.getCourseName());
        simpleCourse.setTeacherRealName(course.getTeacherRealName());
        simpleCourse.setSectionCount(course.getSectionCount());
        simpleCourse.setCourseTime(course.getCourseTime());
        simpleCourse.setMaxStudentCount(course.getMaxStudentCount());
        simpleCourse.setTextbook(course.getTextbook());
        simpleCourse.setCreditsCount(course.getCreditsCount());
        simpleCourse.setAllScore(course.getAllScore());
        simpleCourse.setPassScore(course.getPassScore());
        return simpleCourse;
    }

    @Override
    @Cacheable(value = "teacherCourses", key = "#teacherId")
    public List<Course> searchTeacherCourses(Integer teacherId) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_user_id", teacherId);
        return courseMapper.selectList(queryWrapper);
    }
}




